
#include "json_str.h"

namespace json
{

	str::str(void) : _data((char*)malloc(4)), _size(0), _allocated(4) { }
	str::str(const char * a)
	{
		_size = _c_str_len(a);
		_data = (char*)malloc(_size);
		_allocated = _size;
		_str_cpy(const_cast<char*>(a), _data, _size);
	}
	str::str(const str & a)
	{
		_size = a._size;
		_allocated = a._allocated;
		_data = (char*)malloc(_allocated);
		_str_cpy(a._data, _data, _size);
	}
	str::~str(void)
	{
		free(_data);
	}

	str::operator char *(void)
	{
		return _data;
	}
	str::operator const char *(void)
	{
		return const_cast<const char*>(_data);
	}

	str & str::operator =(const char * a)
	{
		int c_size = _c_str_len(a);
		_mem_assert(c_size);
		//for (int i = 0; i < 
		return *this;
	}
	str & str::operator =(const str & a)
	{ 
		_mem_assert(a._size);
		return *this;
	}
	str & str::operator +=(char a)
	{
		_mem_assert(_size + 1);
		_data[_size++] = a;
		_null_end();
		return *this;
	}
	str & str::operator +=(const char * a)
	{
		int c_size = _c_str_len(a);
		_mem_assert(_size + c_size);
		for (unsigned int i = _size, j = 0; i < _size + c_size; ++i, ++j) _data[i] = a[j]; 
		_size = _size + c_size;
		_null_end();
		return *this;
	}
	str & str::operator +=(const str & a)
	{
		return *this;
	}

	bool str::equals(const str & a)
	{
		if (_size != a._size) return false;
		for (unsigned int i = 0; i < _size; ++i)
		{
			if (_data[i] != a._data[i]) return false;
		}
		return true;
	}
	void str::substr(unsigned int offset, unsigned int length)
	{
		if (offset > _size) return;
		if (length == 0) length = _size - offset;
		for (unsigned int i = offset, j = 0; 
			i < offset + length && i < _size; 
			++i, ++j)
		{
			_data[j] = _data[i];
		}
		_size = length;
		_null_end();
	}
	void str::trim(char a)
	{
	}

}
